import router from './router'
import { __getLocal } from '@/utils/tools.js'
import { useAccountStore } from '@/stores/account'
import { parseUrlParams } from '@/utils/tools.js'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
function routerGo(to, next) {
  next()
  // next({
  //   ...to,
  //   query: {
  //     ...to.query
  //   }
  // })
}
// 配置白名单
export function beforeEach() {
  router.beforeEach(async (to, from, next) => {
    NProgress.start()
    const accountStore = useAccountStore()
    const hasToken = accountStore.TOKEN
    if (hasToken) {
      routerGo(to, next)
    } else {
      const accountStore = useAccountStore()
      let qs = parseUrlParams(window.location.href)
      const { userId } = qs
      try {
        userId ? await accountStore.getToken() : await accountStore.getCCWebToken()
        routerGo(to, next)
      } catch {
        accountStore.resetToken()
        next(false)
      }
    }
  })
  router.afterEach(() => {
    NProgress.done()
  })
}
